package com.wthink.hzgj.util

import android.content.ContentResolver
import android.content.Context
import android.provider.Settings
import android.provider.Settings.SettingNotFoundException


object ScreenSettings {

    /**
     * 判断是否开启了自动亮度调节
     */
    fun isAutoBrightness(context: Context): Boolean {
        val resolver: ContentResolver = context.getContentResolver()
        var automicBrightness = false
        try {
            automicBrightness = Settings.System.getInt(
                resolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE
            ) === Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
        } catch (e: SettingNotFoundException) {
            e.printStackTrace()
        }
        return automicBrightness
    }

    /**
     * 获取屏幕的亮度
     */
    fun getScreenBrightness(context: Context): Int {
        val resolver: ContentResolver = context.getContentResolver()
        var nowBrightnessValue  = 0
        try {
            nowBrightnessValue =  Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (e: SettingNotFoundException) {
            e.printStackTrace()
        }
        return nowBrightnessValue
    }

    /**
     * 开启关闭自动亮度调节
     */

    fun autoBrightness(context: Context,flag: Boolean):Boolean{
        var value = 0
        if (flag) {
            value = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC   //开启
        } else {
            value = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL      //关闭
        }
        return Settings.System.putInt(context.getContentResolver(),
            Settings.System.SCREEN_BRIGHTNESS_MODE,
            value)
    }


    /**
     * 保存亮度设置状态，退出app也能保持设置状态
     */
    fun saveBrightness(context: Context,brightness: Int){
        val resolver: ContentResolver = context.getContentResolver()
        val uriFor = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
        resolver.notifyChange(uriFor,null)
    }
}